<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of simulasi
 *
 * @author miyzan
 */
class simulasi extends MY_Controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model("simulasi_model", "datamodel");
        $this->load->library('../controllers/global_reference');
    }

    public function get_form() {

        $rth = $this->datamodel->get_thajaran_aktif();
        $thajaran = $rth[0]->retval;
        $rth = $this->datamodel->get_semester_aktif($thajaran);
        $semester = $rth[0]->retval;
        $data = array('tglbayar' => date('Y-m-d'), 'id_th_ajaran' => $thajaran, 'semester' => $semester);
        echo '{"success":true,data:' . json_encode($data) . '}';
    }
    
        public function get_form2() {

//        $rth = $this->datamodel->get_thajaran_aktif();
//        $thajaran = $rth[0]->retval;
//        $rth = $this->datamodel->get_semester_aktif($thajaran);
//        $semester = $rth[0]->retval;
        $data = array('tglbayar' => date('Y-m-d'));
        echo '{"success":true,data:' . json_encode($data) . '}';
    }

    public function get_piutang() {
        $thajaran = isset($_POST['id_th_ajaran']) ? $this->db->escape_str($this->input->post('id_th_ajaran', TRUE)) : '';
        $thbl = isset($_POST['thbl']) ? $this->db->escape_str($this->input->post('thbl', TRUE)) : 0;
        $nis = isset($_POST['nis']) ? $this->db->escape_str($this->input->post('nis', TRUE)) : '';
        $result = $this->datamodel->get_piutang_aktif($nis, $thajaran);
        echo $result;
    }
    
    public function get_piutang2() {
//        $thajaran = isset($_POST['id_th_ajaran']) ? $this->db->escape_str($this->input->post('id_th_ajaran', TRUE)) : '';
//        $thbl = isset($_POST['thbl']) ? $this->db->escape_str($this->input->post('thbl', TRUE)) : 0;
        $nis = isset($_POST['nis']) ? $this->db->escape_str($this->input->post('nis', TRUE)) : '';
        $result = $this->datamodel->get_piutang_aktif2($nis);
        echo $result;
    }
    

    public function get_potongan_piutang() {
        $thbl = isset($_POST['thbl']) ? $this->db->escape_str($this->input->post('thbl', TRUE)) : 0;
        $nis = isset($_POST['nis']) ? $this->db->escape_str($this->input->post('nis', TRUE)) : '';
        $kdbiaya = isset($_POST['kd_biaya']) ? $this->db->escape_str($this->input->post('kd_biaya', TRUE)) : '';
        $result = $this->datamodel->get_potongan_piutang($nis, $thbl, $kdbiaya);
        echo $result;
    }

    public function proses_bayar() {
        $nis = isset($_POST['nis']) ? $this->db->escape_str($this->input->post('nis', TRUE)) : '';
        $keterangan = isset($_POST['keterangan']) ? $this->db->escape_str($this->input->post('keterangan', TRUE)) : '';
        $tglbayar = isset($_POST['mtglbayar']) ? $this->db->escape_str($this->input->post('mtglbayar', TRUE)) : NULL;
        $id_th_ajaran = isset($_POST['id_th_ajaran']) ? $this->db->escape_str($this->input->post('id_th_ajaran', TRUE)) : NULL;
        $semester = isset($_POST['semester']) ? $this->db->escape_str($this->input->post('semester', TRUE)) : NULL;
        $data = isset($_POST['postdata']) ? json_decode($this->input->post('postdata', TRUE)) : array();

        $kode = 'TX' . date('Ym');
        $rkode = $this->global_reference->sequence($kode);
        $id = $kode . '-' . $rkode[0]->retval;

        $id_user = $this->session->userdata('username');
        $result = '';
        $spname = 'sp_piutang_bayar';
        foreach ($data as $v) {
            $param = array($id, $tglbayar, $v->thbl, $nis, $id_th_ajaran, $semester, $v->kd_kelas, $keterangan,
                $v->kd_biaya, $v->biaya_dikenakan, $v->total_potongan, $v->detail_potongan, $v->total_biaya,
                $v->jumlah_bayar, $v->saldo, $v->id_piutang_siswa, NULL, $id_user);
            $result = $this->datamodel->SP_execData($spname, $param, true);
        }

        echo $result;
    }
    
    public function proses_bayar2() {
        $nis = isset($_POST['nis']) ? $this->db->escape_str($this->input->post('nis', TRUE)) : '';
        $keterangan = isset($_POST['keterangan']) ? $this->db->escape_str($this->input->post('keterangan', TRUE)) : '';
        $tglbayar = isset($_POST['mtglbayar']) ? $this->db->escape_str($this->input->post('mtglbayar', TRUE)) : NULL;
//        $id_th_ajaran = '';
//        $semester = '';
        $data = isset($_POST['postdata']) ? json_decode($this->input->post('postdata', TRUE)) : array();

        $kode = 'TX' . date('Ym');
        $rkode = $this->global_reference->sequence($kode);
        $id = $kode . '-' . $rkode[0]->retval;

        $id_user = $this->session->userdata('username');
        $result = '';
        $spname = 'sp_piutang_bayar';
        foreach ($data as $v) {
            $param = array($id, $tglbayar, $v->thbl, $nis, $v->id_th_ajaran, $v->semester, $v->kd_kelas, $keterangan,
                $v->kd_biaya, $v->biaya_dikenakan, $v->total_potongan, $v->detail_potongan, $v->total_biaya,
                $v->jumlah_bayar, $v->saldo, $v->id_piutang_siswa, NULL, $id_user);
            $result = $this->datamodel->SP_execData($spname, $param, true);
        }

        echo $result;
    }

    function compareOrder($a, $b) {
        return $a->thbl - $b->thbl;
    }
    public function getDetail_thbl($detail,$thbl,$kd_biaya){
        $arr = array();
        foreach ($detail as $v) {
            if($v->thbl==$thbl && $v->kd_biaya==$kd_biaya){
                array_push($arr, $v);
//                $arr= $v;
            }
        }
        return $arr;
    }

    public function cetak_bukti() {
        $profile = $this->global_reference->get_profile();
        $namasekolah = $profile[0]->nama_sekolah;
        $alamat = $profile[0]->alamat_sekolah;
        $path_url = parse_url($profile[0]->url_logo, PHP_URL_PATH);

        $arr = explode('/', $path_url);
        $urlllogo = '';
        for ($i = 0; $i < count($arr); $i++) {
            if ($i > 1) {
                $urlllogo.=$arr[$i];
                if ($i < count($arr) - 1) {
                    $urlllogo.='/';
                }
            }
        }
        $masterbiaya=$this->datamodel->get_master_biaya();
        
        $data = isset($_GET['data']) ? json_decode($this->input->get('data', TRUE)) : array();
        $detail = isset($_GET['detail']) ? json_decode($this->input->get('detail', TRUE)) : array();
//        echo json_encode($detail);return;
        $data = is_array($data) ? $data : json_decode($data);
        $detail = is_array($detail) ? $detail : json_decode($detail);
//        echo var_dump($detail);
//        usort($detail, array($this,'compareOrder'));
        $arrthbl = array();

        foreach ($detail as $v) {
            if (count($arrthbl) > 0) {
                if (!in_array($v->thbl, $arrthbl)) {
                    array_push($arrthbl, $v->thbl);
                }
            } else {
                array_push($arrthbl, $v->thbl);
            }
        }
        asort($arrthbl);
//        $arr=  $this->getDetail_thbl($detail,'201403','B-1');
//        echo $arr[0]->saldo;
//        return;
        $this->load->library('report_fpdf/BuktiBayar');
//        $pdf = new BuktiBayar('P', 'mm', array(215, 110));
        $pdf = new BuktiBayar('P', 'mm', array(215, 98));
        $pdf->setUrlLogo($urlllogo);
        $pdf->setNamasekolah($namasekolah);
        $pdf->setAlamat($alamat);
        $pdf->setDataheader($data[0]);
//        $pdf->AliasNbPages();
        $pdf->SetFont('Arial', '', 8);
        $pdf->create_pdf($detail,$arrthbl,$masterbiaya);
//        $pdf->create_pdf($filter, $resArr,1);
//        $pdf->create_pdf_nonnol($filter, $resArr,0);
        $pdf->Output("winprintbayar_id", "I");
//        echo $urlllogo;
    }

}

?>
